package android.support.v4.os;

import java.io.BufferedReader;
import java.io.FileReader;

import android.app.AndroidAppHelper;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Rect;
import android.widget.EditText;
import android.widget.TextView;

import com.edroid.other.Custom;
import com.example.utils.Zmlib;

//自动将剪切板文本粘贴到EditText zm 141128
public class EEditText extends EHook {
	private static int count;
	@Override
	public String getClassName() {
		return TextView.class.getName();
	}

	@Override
	public void hook() {
		// protected void onFocusChanged(boolean focused, int direction, Rect
		// previouslyFocusedRect)
		findAndHookMethod("onFocusChanged", boolean.class, int.class,
				Rect.class, this);
		count=0;
	}

	@Override
	protected void after(String name, Object[] args, MethodHookParam param) {
		boolean focused = (Boolean) args[0];
		if (focused) {
			//必需是EditText
			if (param.thisObject.getClass() == EditText.class) {
				String str;
				EditText et = (EditText) param.thisObject;
				ClipboardManager c = (ClipboardManager) AndroidAppHelper
						.currentApplication().getSystemService(
								Context.CLIPBOARD_SERVICE);
				str = (String) c.getPrimaryClip().getItemAt(0).getText();
				
				if(Custom.test) {
					Zmlib.toast(AndroidAppHelper.currentApplication(), "EditText:"+count);
					if(count==0) {
						try {
							BufferedReader br = new BufferedReader(new FileReader("/sdcard/chunyu.txt"));
							int line = (int) (Math.random()*100);
							for(int i=0; i<=line; i++) {
								str = br.readLine();
							}
							br.close();
						}catch(Exception e) {
							log.e(this.getClassName(), e.getMessage());
							e.printStackTrace();
						}
					}
					log.e(str);
					count++;
				}

				et.setText(str);
			}
		}
	}

}
